const Router = require('@koa/router')
const users = require('./users')
const products = require('./products')

const router = new Router()
router
  .get('/', (ctx, next) => {
    ctx.body = 'home'
  })
  .param('id', (id, ctx, next) => {
    console.log(id)
    next()
  })
  .get('/:id', async (ctx, next) => {
    ctx.body = ctx.params
    let result = await next()
    console.log(result, 'result') // koa可以拿到下一个中间件返回的结果
    console.log(home.url('position', {id: 100}, {query: {name: 'lisa'} }))
    ctx.redirect(home.url('position', {id: 100}, {query: {name: 'lisa'} }))
  }).get(['/id', '/name'], async (ctx, next) => {
    ctx.body = ctx.url
    ctx.redirect('/')
  }, (ctx, next) => {
    console.log(0)
    return 'hello'
  })
  .get('position', '/list/:id', (ctx, next) => { //访问 http://localhost:3333/list/100 
    ctx.body = 'route name'
  })
  .use('/users', users.routes(), users.allowedMethods())
  .use('/products', products.routes(), products.allowedMethods())
  .get('/abc/123', async (ctx, next) => {
    console.log('m1 start')
    await next()
    console.log('m1 end')
  }, async (ctx, next) => {
    console.log('m2 start')
    await next()
    console.log('m2 end')
  }, async (ctx, next) => {
    console.log('m3 start')
    await next()
    console.log('m3 end')
  })
  
module.exports = router
